//index.js
//获取应用实例
const app = getApp()

Page({
    data: {
        nowPgae:1,
        page_index: "book",
        startX:0,
        slider:false,
        addClass:"",
        animationData:{},
        cardInfoList: [
          { src: "http://sd.offcn.com/zg/wximg/2022/szyf/book/01.jpg"}, 
          { src: "http://sd.offcn.com/zg/wximg/2022/szyf/book/02.jpg"}, 
          { src: "http://sd.offcn.com/zg/wximg/2022/szyf/book/03.jpg"}, 
          { src: "http://sd.offcn.com/zg/wximg/2022/szyf/book/04.jpg"}, 
          { src: "http://sd.offcn.com/zg/wximg/2022/szyf/book/05.jpg"}, 
          { src: "http://sd.offcn.com/zg/wximg/2022/szyf/book/06.jpg"}, 
          { src: "http://sd.offcn.com/zg/wximg/2022/szyf/book/07.jpg"}, 
          { src: "http://sd.offcn.com/zg/wximg/2022/szyf/book/08.jpg"}, 
          { src: "http://sd.offcn.com/zg/wximg/2022/szyf/book/09.jpg"}, 
          { src: "http://sd.offcn.com/zg/wximg/2022/szyf/book/10.jpg"}, 
          { src: "http://sd.offcn.com/zg/wximg/2022/szyf/book/11.jpg"}, 
          { src: "http://sd.offcn.com/zg/wximg/2022/szyf/book/12.jpg"}, 
          { src: "http://sd.offcn.com/zg/wximg/2022/szyf/book/13.jpg"}, 
          { src: "http://sd.offcn.com/zg/wximg/2022/szyf/book/14.jpg"}, 
          { src: "http://sd.offcn.com/zg/wximg/2022/szyf/book/15.jpg"}, 
          { src: "http://sd.offcn.com/zg/wximg/2022/szyf/book/16.jpg"}, 
          { src: "http://sd.offcn.com/zg/wximg/2022/szyf/book/17.jpg"}, 
          { src: "http://sd.offcn.com/zg/wximg/2022/szyf/book/18.jpg"}, 
          { src: "http://sd.offcn.com/zg/wximg/2022/szyf/book/19.jpg"}, 
          { src: "http://sd.offcn.com/zg/wximg/2022/szyf/book/20.jpg"} 
        ]
    },
    touchstart(e){
        this.setData({
            startX: e.changedTouches[0].clientX,
        })
    },
    touchend(e) {
        let that=this;
        let startX = this.data.startX;
        let endX = e.changedTouches[0].clientX;
        if (this.data.slider)return;

        // 下一页(左滑距离大于30)
        if (startX - endX > 30){
            this.setData({
                slider: true
            });
            //尾页(当前页 等于 总页数)
            if (this.data.nowPgae == this.data.cardInfoList.length){
                this.setData({
                    slider: false
                });
                wx.showToast({title: '已经是最后一张了',icon:'none'});
                return;
            };

            //创建动画   5s将位置移动到-150%,-150%
            let animation = wx.createAnimation({
                duration: 500,
            });
            animation.translateX('-150%').translateY('-150%').rotate(60).step();
            this.setData({
                animationData: animation.export()
            });

            // 移动完成后
            setTimeout(function(){
                var cardInfoList = that.data.cardInfoList;
                var slidethis = that.data.cardInfoList.shift(); //删除数组第一项
                that.data.cardInfoList.push(slidethis); //将第一项放到末尾
                //创建动画   将位置归位
                let animation = wx.createAnimation({
                    duration: 0,
                });
                animation.translateX('-53%').translateY('-55%').rotate(0).step();

                that.setData({
                    cardInfoList: that.data.cardInfoList,
                    animationData: animation.export(),
                    slider:false,
                    nowPgae:that.data.nowPgae+1
                });
            },500)
        }

        // 上一页
        if (endX-startX  > 30){
            this.setData({
                slider: true
            })
            //首页
            if (this.data.nowPgae == 1) {
                this.setData({
                    slider: false
                })
                wx.showToast({title: '已经到第一张了',icon: 'none'})
                return;
            };

            //创建动画  移动到-150%,-150%
            let animation = wx.createAnimation({
                duration: 0,
            });
            animation.translateX('-150%').translateY('-150%').rotate(100).step();


            var cardInfoList = that.data.cardInfoList;
           
            var slidethis = that.data.cardInfoList.pop(); //删除数组末尾项
            that.data.cardInfoList.unshift(slidethis);//将删除的末尾项放到第一项
            that.setData({
                animationData: animation.export(),
                cardInfoList: that.data.cardInfoList,
            });
            
            setTimeout(function(){
                //创建动画   5s将位置移动到原位
                let animation2 = wx.createAnimation({
                    duration: 500,
                    // timingFunction: 'cubic-bezier(.8,.1,.2,0.8)',
                });
                animation2.translateX('-53%').translateY('-55%').rotate(0).step();
                that.setData({
                    animationData: animation2.export()
                });
                that.setData({
                    slider: false,
                    nowPgae: that.data.nowPgae - 1
                });
            },50)
        }
    },

    onLoad: function (options) {
      var that = this
      var phone = wx.getStorageSync('phone');
      console.log(phone);
      if(options.area){
        wx.setStorageSync('area', options.area)
        wx.setStorageSync('agent', options.agent)
        wx.setStorageSync('page', options.page)
      }
      if(phone){
        wx.showToast({
          title: '已登录直接查看',
          duration: 2000,
        })
         that.setData({
          "addClass": "move"
         })
      }
    },
    
    getPhoneNumber(e) {
      var _ = this;
      var area = wx.getStorageSync('area')
      var agent = wx.getStorageSync('agent')
      var page = wx.getStorageSync('page')
      var flag = _.data.flag;
      if(flag){
        wx.cloud.callFunction({
        name: 'getMobile',
        data: {
          weRunData: wx.cloud.CloudID(e.detail.cloudID),
        }
      }).then(res => {
        _.setData({
          mobile: res.result,
        })
        console.log(res.result)
        wx.request({
          url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/collect_v3',
          method: 'POST',
          data: {
            activeId: 32,
            mobile: res.result,
            areaCode: area,
            agent: agent,
            pageId: page
          },
          success: res => {
            console.log(res);
          }
        })
  
        wx.setStorageSync('phone', _.data.mobile)
        wx.request({ //发送请求
          url: 'https://zg99.offcn.com/index/biaodan/register?actid=49031&callback=?', //接收url
          data: {
            phone: res.result,
            fenxiao: area,
            geneal: agent,
            isagree: true
          }, //接收参数
          success(res) {
            var jsonStr = res.data;
            jsonStr = jsonStr.replace("(", "");
            jsonStr = jsonStr.replace(")", "");
            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
              var theJson = JSON.parse(jsonStr);
            }
            console.log(theJson)
            _.setData({
              "addClass": "move"
            })
          }
        });
  
      })} else {
        wx.showToast({
          title: '请先勾选隐私协议！',
          icon: 'none'
        })
      }
  
    },
    
    xz: function (e) {
      var _ = this;
      var flag = e.detail.value[0];
      if (flag == undefined) {
        flag = false
      } else {
        flag = true
      }
      _.setData({
        flag
      })
      console.log(_.data.flag)
    },
    xy: function (e) {
      var _ = this;
      wx.navigateTo({
        url: '../agreement/agreement',
      })
    },
})